package com.congwulin.wechat.util.service.vo;

import com.congwulin.wechat.entity.common.SuperVO;
import com.congwulin.wechat.util.EntityConst;
import com.congwulin.wechat.util.interfaces.AbstractVOService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class VODeleteService extends AbstractVOService{


    /**
     * 实体抽象服务构造函数
     *
     * @param dao 实体保存数据库器
     */
    public VODeleteService(VOPersistent dao) {
        super(dao);
    }

    /**
     * 删除实体数组
     *
     * @param vos 实体数组
     */
    public void delete(SuperVO[] vos) {
        List<SuperVO> list = Arrays.asList(vos);
        this.delete(list);
    }

    /**
     * 删除实体列表
     *
     * @param list 一主多子实体列表
     */
    @SuppressWarnings("unchecked")
    public void delete(List<?> list) {
        List<SuperVO> paralist = (List<SuperVO>) list;

        for(SuperVO vo:paralist){
            vo.setStatus(EntityConst.DELETE);
        }
        Map<String,String> lockmap = null;
        try {
            // 加锁、校验时间戳 TODO
            lockmap = this.lock(paralist);
            this.checkTs(paralist);

            this.getDAO().delete(paralist);
        }
        finally {
            //释放锁 TODO
            this.unlock(lockmap);
        }
    }
}
